﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WebDomain.Abstract;
using WebDomain.Entities;

namespace WebDomain.Concrete
{
    public class EFProductRepository : IProductRepository
    {
        private CZTopComputersEntities context = new CZTopComputersEntities();

        public IQueryable<Entities.Product> Products
        {
            get { return context.Products; }
        }

        public void SaveProduct(Product product)
        {
            if(product.ID==0)
            {
                context.Products.AddObject(product);
            }
            else
            {
                var prod = context.Products.First(p => p.ID == product.ID);
                DeleteProduct(prod);
                context.Products.AddObject(product);
            }
            context.SaveChanges();
        }

        public void DeleteProduct(Product product)
        {
            context.Products.DeleteObject(context.Products.FirstOrDefault(p=>p.ID==product.ID));
            context.SaveChanges();
        }
    }
}
